package com.maserb.monitor.service.jwOur.impl;

import com.maserb.monitor.common.CreateTimeUtils;
import com.maserb.monitor.entity.jwOur.JwFilePhotoEntity;
import com.maserb.monitor.model.JwFileInfoModel;
import com.maserb.monitor.repository.jwOur.JwFilePhotoRepository;
import com.maserb.monitor.service.base.impl.JpaSpecificationServiceImpl;
import com.maserb.monitor.service.jwOur.JwFilePhotoService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class JwFilePhotoServiceImpl extends JpaSpecificationServiceImpl<JwFilePhotoRepository,
        JwFilePhotoRepository, JwFilePhotoEntity, String>
        implements JwFilePhotoService {
    @Value("${monitor.uploadDir}")
    private String uploadDir;

    @Override
    public String getUploadDir() {
        return uploadDir;
    }

    //图片
    @Override
    public JwFileInfoModel uploadImage(MultipartFile multipartFile) throws IOException {
        String dirPath = this.getUploadDir()+"messageImage\\";
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String splitDirPath =sdf.format(date);
        String relativeFilename = FilenameUtils.concat(dirPath, splitDirPath);
        File dir = new File(relativeFilename);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        splitDirPath ="messageImage/"+ splitDirPath;
        String extension = FilenameUtils.getExtension(multipartFile.getOriginalFilename());
        String filename = UUID.randomUUID() + "." + extension;
        String realFilename = FilenameUtils.concat(splitDirPath, filename);
        File serverFile = new File(FilenameUtils.concat(relativeFilename, filename));
        multipartFile.transferTo(serverFile);
        JwFilePhotoEntity entity = new JwFilePhotoEntity();
        entity.setPath(realFilename.replaceAll("\\\\","/"));
        entity.setCreateTime(CreateTimeUtils.getCreateTime());
        entity.setFileExtension(extension);
        entity.setType(1);
        Long size = multipartFile.getSize();
        entity.setSize(getFileSize(size));
        entity.setName(multipartFile.getOriginalFilename());
        this.repository.save(entity);
        JwFileInfoModel model = new JwFileInfoModel();
        model.setId(entity.getId());
        model.setPath(entity.getPath());
        return model;
    }

    public  String getFileSize(long size){
        if (size < 1024) {
            return String.valueOf(size) + "B";
        } else {
            size = size / 1024;
        }
        //如果原字节数除于1024之后，少于1024，则可以直接以KB作为单位
        //因为还没有到达要使用另一个单位的时候
        //接下去以此类推
        if (size < 1024) {
            return String.valueOf(size) + "KB";
        } else {
            size = size / 1024;
        }
        if (size < 1024) {
            //因为如果以MB为单位的话，要保留最后1位小数，
            //因此，把此数乘以100之后再取余
            size = size * 100;
            return String.valueOf((size / 100)) + "."
                    + String.valueOf((size % 100)) + "MB";
        } else {
            //否则如果要以GB为单位的，先除于1024再作同样的处理
            size = size * 100 / 1024;
            return String.valueOf((size / 100)) + "."
                    + String.valueOf((size % 100)) + "GB";
        }
    }
}